package sk.stuba.fiit.ltasys.web.mvc.model.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import sk.stuba.fiit.ltasys.web.mvc.model.entity.ArchivePackageEntity;
import sk.stuba.fiit.ltasys.web.mvc.model.service.ArchiveService;
import sk.stuba.fiit.ltasys.web.mvc.model.service.NightDarkJob;
import sk.stuba.fiit.ltasys.web.mvc.model.service.core.ArchiveCreator;
import sk.stuba.fiit.ltasys.web.mvc.model.service.core.ArchiveSignatureVerifier;

public class NightDarkJobImpl implements NightDarkJob {

	@Autowired
	private ArchiveCreator archiveCreator;
	
	@Autowired 
	private ArchiveSignatureVerifier archiveSignature;
	
	@Autowired
	private ArchiveService archiveService;
	
	public void execute() throws Exception {
		System.out.println("Retimestamping start " +System.currentTimeMillis());
		List<ArchivePackageEntity> archivPackages = archiveService.getAllRecordForRetimestamping();
		
		int counter=0;
		for (ArchivePackageEntity entity : archivPackages) {
			System.out.println("Retimestamping data with token: "+entity.getToken());
				archiveSignature.verifieArchiveSignature(entity);
				archiveCreator.prolongProtection(entity);
				archiveService.saveArchivePackage(entity);
				archiveSignature.verifieArchiveSignature(entity);
				counter++;
		}		
		System.out.println(counter + " Record was retimestamped");
		System.out.println("Retimestamping end " +System.currentTimeMillis());
	}
}
